import dayjs from "dayjs";
import ProxyList from "free-proxy";
import { Logger } from "mongodb";

import { ExtractClientInterface, ProxyInterface } from "../etl";
import { getLogger } from "../utils/log4j";


const logger = getLogger(`free-proxy`)
export class FreeProxy implements ExtractClientInterface {

	async * extract(): AsyncGenerator<ProxyInterface, void, any> {
		const proxyList = new ProxyList()
		const list: ProxyList.IFreeProxy[] = await proxyList.get()
		for (const item of list) {
			const ip = item.ip
			const port = Number(item.port)
			const protocol = item.protocol
			if (!ip || !port || !protocol) {
				logger.error(`proxy wrong format or empty value`)
				continue
			}
			const proxy: ProxyInterface = {
				ip: ip,
				port: port,
				protocol: protocol
			}
			yield proxy
		}
	}
}


